layui.use('table', function(){
  var table = layui.table;
   
   
  //监听工具条
  table.on('tool(demo)', function(obj){
    var da = obj.data;
    //删除
    if(obj.event === 'del'){
      layer.confirm('确认删除吗?', function(index){
    	layer.close(index);
        obj.del();
        var url="../../listenPurchase/deleteMateria.action";
  	 	var data={"purchaseId":da.purchaseId};
  	    $.post(url,data,function(mes){
  		  if(mes>0){
			layer.msg("删除成功");
  		  }
  	  },"json")
      });
    //修改
    } else if(obj.event === 'edit'){
    	layer.open({
  		  type: 2, //设置为iframe
  		 area: ['100%', '74%'],
		  offset: '5%'
  		  ,content: ['PurchaseRawstock.html'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
  		  ,success: function(layero, index){
  			var url="../../listenPurchaseDetail/materialDetailFindAll.action";
  	    	var data={"purchaseId":da.purchaseId};
  	    	var body = layer.getChildFrame('body', index);
			var iframe = window[layero.find('iframe')[0]['name']];
			 
  	    	$.post(url,data,function(mes){
  	    		 //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
				
				$.each(mes.data,function(i,item){
					 
					iframe.$("#rawstockIdText").val(iframe.$("#rawstockIdText").val()+(item.vocationalWorkId+","))
					iframe.$("#purchaseText").val(iframe.$("#purchaseText").val()+item.vocationalWorkId+","+item.pdName+","+item.pdPrice+","+item.pdNumber+","+item.pdSubtotal+","+"#")	 
					iframe.load();
				})
				//调用子页面渲染的方法 刷新
				
  	    	},"json");
  	    	 
  	    	
  	    	var url="../../listenPurchase/selectPurchaseId.action";
  	    	var data={"purchaseId":da.purchaseId};
  	    	 
  	    	$.post(url,data,function(mes){
				iframe.$("#sumNumber").val(mes.purchaseNumber);		 
				iframe.$("#sumPrice").val(mes.purchasePrice);	
				iframe.$("#purchaseId").val(mes.purchaseId);	
				iframe.$("#explain").val(mes.purchaseExplain);	
				iframe.$("#suId").val(mes.supplierId);	
				//调用子页面渲染的方法 刷新
				iframe.load();
  	    	},"json");
  	    	 
  		  }
    	
  	  }); 
    	 
    }else if(obj.event === 'detail'){
    	layer.open({
    		 title: "原材料采购明细",
    		  type: 2, //设置为iframe
    		  area: ['100%', '400px'],
    		  content: ['purchaseDetail.html?purchaseId='+da.purchaseId] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
    	  }); 
    }else if(obj.event === 'quality'){
    	layer.confirm('确定提交质检?', function(index){
    		var index = layer.load(0, {
  			  shade: [0.1,'#fff'] //0.1透明度的白色背景
  			});
	    	var submitString=da.purchaseId+","+da.purchaseNumber+","+1;
	    	var url="../../listenQualityTesting/addQualityTesting.action";
	    	var data={"submitString":submitString};
		  	  $.post(url,data,function(mes){
		  		   if(mes>0){
		  			 layer.close(index);
		  			 layer.msg("提交质检成功")
		  			 location.reload();
		  		   }
		  	  },"json")
    	});
    }else if(obj.event === 'auditing'){
    	layer.confirm('确定提交审核?', function(index){
		var index = layer.load(0, {
			  shade: [0.1,'#fff'] //0.1透明度的白色背景
			});
    	 var url="../../listenPurchase/updatePurchaseState.action";
    	 var data={"purchaseId":da.purchaseId,"auditingStatus":1};
	  	  $.post(url,data,function(mes){
	  		   if(mes.datas==0){
	  			 layer.close(index);
	  			 layer.msg("提交审核成功")
	  			 location.reload();
	  		   }
	  	  },"json")
    	});
    }
    
    
  });
   
  
  //表单重载
  var $ = layui.$, active = {
		    reload: function(){
		      var supplierId = $('#supplierId');
		      
		      //执行重载    查询
		      table.reload('purchase', {
		        page: {
		          curr: 1 //重新从第 1 页开始
		        }
		        ,where: {
		        	supplierId: supplierId.find("option:selected").text()
		        }
		      });
		    }
		  };
  
  $('.demoTable #search').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
 
 
  
  /**
  * 增加
  */
  $('#insert').on('click', function(){
	  layer.open({
		  type: 2, //设置为iframe
		  area: ['100%', '74%'],
		  offset: '5%'
		  ,content: ['PurchaseRawstock.html'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
		  
	  	   
	  }); 
	          
  });
});

layui.use(['form', 'table'], function(){
	  var form = layui.form
	  var table = layui.table
	  var url="../../listenSupplier/findSupplier.action";
	  var data=null;
	  $.post(url,data,function(mes){
		  $.each(mes.data,function(i,item){
				  $("#supplierId").append("<option value="+item.suName+">"+item.suName+"</option>")
				  load();
		  })
	  },"json")
	  
});
function load(){
	var form = layui.form;
	form.render();
}
